/**

 * @author admin

 * 2014年7月16日

 */

package com.tmall.testgen.plugin.wizards.runinspring.listener;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;

import com.tmall.testgen.client.ClientDataCenter;
import com.tmall.testgen.client.commond.impl.SendCommondImpl;
import com.tmall.testgen.client.config.TargetProjectConfig;
import com.tmall.testgen.client.runinspring.RunInSpringHandler;
import com.tmall.testgen.common.ConnectionCommond;
import com.tmall.testgen.common.util.SocketUtil;
import com.tmall.testgen.plugin.ConsoleFactory;
import com.tmall.testgen.plugin.wizards.MyMessageDialog;
import com.tmall.testgen.plugin.wizards.runinspring.ComCreator;
import com.tmall.testgen.plugin.wizards.runinspring.RunInSpringWizadPage;
import com.tmall.testgen.transfer.startor.TransferStartorForPlugin;

public class StartSpringBtnSelectListener implements SelectionListener {

	private Text configText;
	private Map<String, Control> btnMap;
	private RunInSpringWizadPage page;

	public StartSpringBtnSelectListener(Text configText,
			Map<String, Control> btnMap, RunInSpringWizadPage page) {
		this.configText = configText;
		this.btnMap = btnMap;
		this.page = page;
	}

	@Override
	public void widgetSelected(SelectionEvent e) {
		
		final String config = configText.getText().trim();
		if(config==null||config.length()==0){
			MyMessageDialog.openWarnDialog(page.getShell(), "亲，咱能淡定点不，先选择下配置文件呗！");
			return;
		}
		
		
		IRunnableWithProgress op = new IRunnableWithProgress() {
			volatile boolean isExecuteStart = false;

			public void run(final IProgressMonitor monitor)
					throws InvocationTargetException {
					new Thread(new Runnable() {

						@Override
						public void run() {
							monitor.beginTask("start ", 100);
							for (int i = 1; i < 20; i++) {
								monitor.worked(5);
								if (isExecuteStart)
									break;
								try {
									Thread.sleep(1000);
								} catch (InterruptedException e) {
									ConsoleFactory.putError(e);
									e.printStackTrace();
								}
							}

						}
					}).start();
					try{
						RunInSpringHandler.getInstance().changeConfigLoacation(TargetProjectConfig.getInstance().getTargetProjectName(),TargetProjectConfig.getInstance().getTargetProjectPath(), config);
						
						
						if(SocketUtil.isPortAvailable(ConnectionCommond.Transfer_Server_Port)){
			                String transferJarPath=Platform.getInstallLocation().getURL().getPath()+"plugins/testgen";
			               TransferStartorForPlugin.runCmd(transferJarPath);
			            }
			            
			            if(ClientDataCenter.getInstance().getMockDes()!=null&&ClientDataCenter.getInstance().getMockDes().length()>0){
    			            String ip="127.0.0.1";
    			            ClientDataCenter.getInstance().setIp(ip);
    			            new SendCommondImpl().tryAttatch(ip, ClientDataCenter.getInstance().getPid());
			            }
						isExecuteStart = true;
					}catch(Throwable e){e.printStackTrace();
						ConsoleFactory.putError(e);
						InvocationTargetException ee=new InvocationTargetException(e);
						ee.setStackTrace(e.getStackTrace());
						throw ee;
						
					}
				 finally {
					monitor.done();
				}
			}
		};
		try {
			page.getWizard().getContainer().run(true, false, op);
			// 启动成功 设置按钮状态
			btnMap.get(ComCreator.START_CONFIG_ID).setEnabled(false);
			// btnMap.get(ComCreator.STOP_CONFIG_ID).setEnabled(true);
			btnMap.get(ComCreator.UPDATE_CONFIG_ID).setEnabled(false);

			btnMap.get(ComCreator.COMPUTE_BTN_ID).setEnabled(true);
			btnMap.get(ComCreator.ADD_TO_TEST_BTN_ID).setEnabled(true);
			
			btnMap.get(ComCreator.HSF_CONFIG_BTN_ID).setEnabled(false);
			btnMap.get(ComCreator.HSF_CONFIG_SELECT_BTN_ID).setEnabled(false);
			btnMap.get(ComCreator.HSF_CONFIG_TEXT_ID).setEnabled(false);
			btnMap.get(ComCreator.MOCK_TEXT_ID).setEnabled(false);
			
		} catch (Exception e1) {
			MyMessageDialog.openWarnDialog(page.getShell(), "启动异常，请查看控制台");

		}

	}
	
	

	@Override
	public void widgetDefaultSelected(SelectionEvent e) {

	}

	
}
